# Matplotlib学习综合案例
import numpy as np
import math
from matplotlib import pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']  # 中文显示


snow_area=[83.1,350.7,5903.3,2716.2,4446.5,2975,4615.1 ,8543,137.1, 5073,980.8,5603.9,829.8,3367.1,7166.3,9521.1,3931.4,4479.5,2077.3,2594.3,289.3,1260.9,4649.5,1816.5,3255,176.2,2548.7,1966.9,159.4,579.9,2297.1]
total_output=[52.3,194.2,3321.9,1232.9,2492.3,2017.1,3601.6,5435.2,98.6,3360.6,656,3252.5,496.1,2029.9,4505.2,5777,2428.5,2805.2,1170.8,1420.6,149.2,806.2,2846.6,855.5,1567.7,99.8,1114.2,883.1,61.1,331.8,1474.8]
per_hectare=[6296,5538,5627,4539,5605,6780,7804,6362,7190,6624,6689,5804,5978,6028,6287,6068,6177,6262,5636,5476,5157,6394,6122,4710,4816,5663,4371,4490,3832,5721,6420]
area=['北京','天津','河北','山西','内蒙古','辽宁','吉林','黑龙江','上海','江苏','浙江','安徽','福建','江西','山东','河南','湖北',
'湖南','广东','广西','海南','重庆','四川','贵州','云南','西藏','陕西','甘肃','青海','宁夏','新疆']

# 计算最大值、均值、中位数、标准差
# 最大值
snow_area_max = max(snow_area)
total_output_max = max(total_output)
per_hectare_max = max(per_hectare)

# 均值
snow_area_mean = sum(snow_area)/len(snow_area)
total_output_mean = sum(total_output)/len(total_output)
per_hectare_mean = sum(per_hectare)/len(per_hectare)

# 中位数
def median(List):
    List = sorted(List)
    if len(List)%2==1:
        return List[len(List)//2]
    else:
        return (List[len(List)//2]+List[len(List)//2-1])/2

snow_area_median = median(snow_area)
total_output_median = median(total_output)
per_hectare_median = median(per_hectare)

# 标准差
def stdev(List):
    mean = sum(List)/len(List)
    Sum = 0
    for item in List:
        Sum += (item - mean)**2
    Sum /= len(List)
    return math.sqrt(Sum)

snow_area_stdev = stdev(snow_area)
total_output_stdev = stdev(total_output)
per_hectare_stdev = stdev(per_hectare)


# 绘制柱状图
def draw_bar():
    plt.figure(figsize=(20,10))  # 调整画布大小，适应文字
    plt.bar(range(1,len(snow_area)+1),snow_area)
    plt.xticks(range(1,len(snow_area)+1),area)
    plt.xlabel('省份')
    plt.ylabel('降雪面积')
    plt.title('各省江雪面积')
    plt.show()

# 绘制饼图
def draw_pie():
    plt.figure(figsize=(18,18))
    plt.pie(snow_area,labels=area,autopct='%1.1f%%')
    plt.title('各省降雪面积占比饼图')
    plt.show()

# 绘制气泡图（降雪面积、降雪总量、单位降雪量）
def draw_bubble():
    per_hectare_down = [item/40 for item in per_hectare]  # 等比缩小数据，防止气泡过大
    plt.scatter(snow_area,total_output,s=per_hectare_down)
    plt.xlabel('降雪面积')
    plt.ylabel('降雪总量')
    plt.title('降雪面积/总量/单位降雪量气泡图')
    plt.show()



if __name__ == "__main__":
    draw_bar()
    draw_pie()
    draw_bubble()
    
